<?php

class ProductsModule extends fvModule {
	
	function __construct () {
	    $moduleName = strtolower(substr(__CLASS__, 0, -6));

	    parent::__construct(fvSite::$fvConfig->get("modules.{$moduleName}.smarty.template"), 
	                        fvSite::$fvConfig->get("modules.{$moduleName}.smarty.compile"), 
	                        fvSite::$Layoult);
	}

	function showIndex($params) {
	    $request = fvRequest::getInstance();
	    $currentSection = $request->getEscapedParameter('section_title');
	    $Path = SectionsManager::getInstance()->getPath($currentSection);
	    $SectionsTree = SectionsManager::getInstance()->getSections($currentSection);
	    $Parent[] = 'null';
	    foreach ($SectionsTree as $ST){
	    	$Parent[] = $ST['id'];	    
	   	}
		$fvTree = new fvTree(SectionsManager::getInstance()->getSectionTree());
	    $sections_options[0] = 'Усі категорії';
	    $sections_options += $fvTree->return_tree2();
        $this->__assign('SectionsOptions', $sections_options);
	    $Products = ProductsManager::getInstance()->getAll('parent in ("'.$currentSection.'", "'.implode('","',$Parent).'")');
	    $this->__assign("Path", $Path);
	    $this->__assign("Products", $Products);
	    return $this->__display("products_list.tpl");
	}
	
	function showSearch($params) {
		$request = fvRequest::getInstance();
    	$sort = $request->getRequestParameter("sort");
		$pp = $request->getRequestParameter("pp");
		if ($pp){
			fvSite::$fvSession->set("pp", $pp);
		}
		else{
			fvSite::$fvSession->set("pp", 10);
		}
    	switch ($sort){
			case 'title' : $sort = 'title'; $direction = 'ASC'; break;
			case 'price' : $sort = 'price'; $direction = 'ASC'; break;
			case 'date' : $sort = 'update_date'; $direction = 'DESC'; break;
			default : $sort = 'update_date'; $direction = 'DESC'; break;
		}
	    $SearchSection = $request->getEscapedParameter('category_id');
	    $SearchText = $request->getEscapedParameter('text');
	    $Path = SectionsManager::getInstance()->getPath($SearchSection);
	    $category='';
	    if ($SearchSection)
	    {
		    $params_tmp[] = $SearchSection;
	        $result[] = $SearchSection;
		    do{
	        	$Sections = SectionsManager::getInstance()->getAll("parent_id in (".implode(',', $params_tmp).")");
	            $params_tmp = array ();
	            if (count($Sections)){
	                foreach ($Sections as $Section){
	                	$params_tmp[] = $Section->getPk();
	                	$result = array_unique(array_merge($params_tmp, $result));    		
	                }
	            }
	            else{
	            	$stop = 0;
	            } 
	        } while ($stop!=0);
	        $category = 'category_id in ("'.$SearchSection.'", "'.implode('","',$result).'") and ';  
		}
		
		
	   	$fvTree = new fvTree(SectionsManager::getInstance()->getSectionTree());
	    $sections_options[0] = 'Усі категорії';
	    $sections_options += $fvTree->return_tree2();
        $this->__assign('SectionsOptions', $sections_options);
        $this->__assign('search_category', $request->getEscapedParameter('category_id'));
        $this->__assign('search_text', $request->getEscapedParameter('text'));

        $this->__assign("pp", fvSite::$fvSession->get("pp"));
        $this->__assign("request", $request);
	    $this->__assign("PerPage", array("10" => "10","20" => "20","30" => "30","40" => "40","50" => "50",));
        $pager = new fvPager(ProductsManager::getInstance());
	    $this->__assign("Products", $pager->paginate($category.'(title like "%'.$SearchText.'%" or description like "%'.$SearchText.'%")', $sort.' '.$direction));
	    
	    $this->__assign("Path", $Path);
	    
	    return $this->__display("products_list.tpl");
	}
	
	function showOne($params) {
	    $request = fvRequest::getInstance();
	    $Product = ProductsManager::getInstance()->getByPk($request->getRequestParameter('prod_id'));
	    $fvTree = new fvTree(SectionsManager::getInstance()->getSectionTree());
	    $sections_options[0] = 'Усі категорії';
	    $sections_options += $fvTree->return_tree2();
        $this->__assign('SectionsOptions', $sections_options);
	    $Path = SectionsManager::getInstance()->getPath($Product->category_id);
	    $SaleProducts = ProductsManager::getInstance()->getAll(null, "RAND()", '0,3');
	    $query = "select filter_id, val from krshop_product_filters where product_id=?";
        $tmp = fvSite::$DB->getAll($query, array($Product->getPk()));
		foreach ($tmp as $k => $t){
			$Filter = FilterManager::getInstance()->getByPk($t[filter_id]);
	        $ProductFilters[$k][title] = $Filter->title;  
	        $ProductFilters[$k][val] = $Filter->posible_values[$t[val]];
	    }
	    $this->__assign("SaleProducts", $SaleProducts);
	    $this->__assign("Product", $Product);
	    $this->__assign("ProductFilters", $ProductFilters);
	    $this->__assign("Path", $Path);
	    
	    return $this->__display("products_one.tpl");
	}
	
	function showCategory($params) {
		$request = fvRequest::getInstance();
    	$sort = $request->getRequestParameter("sort");
		$pp = $request->getRequestParameter("pp");
		if ($pp){
			fvSite::$fvSession->set("pp", $pp);
		}
		else{
			fvSite::$fvSession->set("pp", 10);
		}
    	switch ($sort){
			case 'title' : $sort = 'title'; $direction = 'ASC'; break;
			case 'price' : $sort = 'price'; $direction = 'ASC'; break;
			case 'date' : $sort = 'update_date'; $direction = 'DESC'; break;
			default : $sort = 'update_date'; $direction = 'DESC'; break;
		}
	    $Path = SectionsManager::getInstance()->getSectionIDs($request->getRequestParameter('cat_id'));
	    $filter = $request->getRequestParameter('filter');
	    $additional = '';
	    if ($filter){
	    	foreach ($filter as $k=>$value){
	    		$filter_ids[] = $k;
	    		foreach ($value as $k1=>$v1){
	    			$filter_vals[] = $k1;
	    		}
	    	}
	    	$query = "SELECT product_id FROM krshop_product_filters WHERE filter_id in (".implode(',', $filter_ids).") AND val in (".implode(',', $filter_vals).")";
	    	$res = fvSite::$DB->getCol($query, 0);
	    	if ($res){
	    		$additional = "and id in (".implode(',', $res).")";
	    	}
	    	else{
	    		$additional = 'and 1!=1';
	    	} 
	    }
	    $pager = new fvPager(ProductsManager::getInstance());
	    $this->__assign("Products", $pager->paginate("category_id in (".implode(',', $Path).") ".$additional , $sort.' '.$direction));
	    $Path = SectionsManager::getInstance()->getPath($request->getRequestParameter('cat_id'));
	    $fvTree = new fvTree(SectionsManager::getInstance()->getSectionTree());
	    $sections_options = $fvTree->return_tree2();
        $this->__assign('SectionsOptions', $sections_options);
        $this->__assign('search_category', $request->getRequestParameter('cat_id'));
        if (SectionsManager::getInstance()->getByPk($request->getRequestParameter('cat_id'))->filters)
	    $Filters = FilterManager::getInstance()->getAll('id in ('.SectionsManager::getInstance()->getByPk($request->getRequestParameter('cat_id'))->filters.')');
	    $this->__assign("Filters", $Filters);
	    $this->__assign("Path", $Path);
	    $this->__assign("pp", fvSite::$fvSession->get("pp"));
        $this->__assign("request", $request);
	    $this->__assign("PerPage", array("10" => "10","20" => "20","30" => "30","40" => "40","50" => "50",));
	    return $this->__display("products_list.tpl");
	}
	
}

?>
